package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.contract.CollectContract;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqPageBean;
import com.ewzj.bigdata.mvp.model.entity.reqbeans.ReqcollectBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseListData;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.google.gson.Gson;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import com.jess.arms.utils.DataHelper;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.functions.Function;


@ActivityScope
public class CollectModel extends BaseModel implements CollectContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public CollectModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<List<CollectBean>> loadCollectData(int page) {
        String userid= DataHelper.getStringSF(mApplication, AppConstants.USERID);
        ReqPageBean reqPageBean=new ReqPageBean(page,AppConstants.PAGE_NUM,userid);
        return service.getCollects(Apputils.json2Body(mGson.toJson(reqPageBean)))
                .map(new Function<BaseData<BaseListData<CollectBean>>, List<CollectBean>>() {
                    @Override
                    public List<CollectBean> apply(BaseData<BaseListData<CollectBean>> baseListDataBaseData) throws Exception {
                        return baseListDataBaseData.getData().getList();
                    }
                });
    }

    @Override
    public Observable<CollectBean> addCollectData(String title) {
        ReqcollectBean reqcollectBean=new ReqcollectBean(title);
        return service.createCollect(Apputils.json2Body(mGson.toJson(reqcollectBean)))
                .map(new Function<BaseData<CollectBean>, CollectBean>() {
                    @Override
                    public CollectBean apply(BaseData<CollectBean> collectBeanBaseData) throws Exception {
                        return collectBeanBaseData.getData();
                    }
                });
    }

    @Override
    public Observable<BaseData<CollectBean>> delCollectData(String id) {
        ReqcollectBean reqcollectBean=new ReqcollectBean();
        reqcollectBean.setId(id);
        return service.delCollect(Apputils.json2Body(mGson.toJson(reqcollectBean)));
    }

    @Override
    public Observable<CollectBean> updateCollectData(String title, String id) {
        ReqcollectBean reqcollectBean=new ReqcollectBean(title,id);
        return service.updateCollect(Apputils.json2Body(mGson.toJson(reqcollectBean)))
               .map(new Function<BaseData<CollectBean>, CollectBean>() {
                   @Override
                   public CollectBean apply(BaseData<CollectBean> collectBeanBaseData) throws Exception {
                       return collectBeanBaseData.getData();
                   }
               });
    }
}